< previous page page_588 next page >

Page 588
Another way of working with multiple records forms an implied loop. Most data entry or data viewing programs include command buttons on the form to move to the next record or previous record. When users repeatedly click these buttons, they execute a type of program loop by repeating the move events. Special considerations for this type of loop are what to do when you are at the first record, the last record, or if you have an empty recordset. The problem is that if you move backward from the first record, forward from the last record, or try to move anywhere in an empty recordset, an error occurs. Fortunately, the Jet database engine provides some help in this area. Some properties of the recordset can tell you when these conditions exist, as described in the following section.
You can use four main recordset properties to control the processing of multiple records in a recordset. Table 26.5 gives the definitions of these properties.
Table 26.5 Properties Used to Control Loop Processing
PropertyDescription
BOFBeginning-of-file flag, indicates whether the record pointer is positioned before the first record (BOF = True) or not (BOF = False).
EOFEnd-of-file flag, indicates whether the record pointer is positioned past the last record (EOF = True) or not (EOF = False).
RecordCountIndicates the number of records in the recordset that have been accessed. This number gives a count of the total records in the recordset only after the last record is accessed (for example, by using MoveLast), unless the recordset in question is a table-type recordset.
NoMatchIndicates that the last Find method or Seek method was unsuccessful in locating a record that matched the desired criteria.

You can use these properties to terminate loops or prevent errors. Consider the data entry form in Figure 26.3. To prevent an error from occurring when users clicks the Next Record button, use code that allows the move only if the recordset is not at the end of the file. The following code takes this possibility into account:
If Not rsTitles.EOF Then
        rsTitles.MoveFirst
        If rsTitles.EOF Then
            rsTitles.MoveLast
        End If
    End If
    Call ShowFields chuck
Alternatively, you can disable the Next button when you reach the end of the file. You can apply one of these principles to the Previous button and the BOF condition. You might also want to check the RecordCount property of a recordset and enable only the Add Record button if the count is zero.

 
< previous page page_588 next page >